/*
 * Copyright 2014 trinea.cn All right reserved. This software is the confidential and proprietary information of
 * trinea.cn ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with trinea.cn.
 */
package com.twotiger.and.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView.ScaleType;
import app.twotiger.p2p.R;

import com.lidroid.xutils.BitmapUtils;
import com.twotiger.and.Constants;
import com.twotiger.and.activity.WebViewPage;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.bean.ImageBean;
import com.twotiger.and.util.CacheUtils;
import com.twotiger.and.util.DownPicUtil;
import com.twotiger.and.util.StringUtils;
import com.twotiger.and.view.RoundImageView;

/**
 * ImagePagerAdapter
 *
 * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2014-2-23
 */
public class HomePopAdapter extends PagerAdapter {

    private BaseActivity context;
    private List<ImageBean> imageIdList;
    private BitmapUtils bitmapUtils;
    private AlertDialog mDialog;

    public HomePopAdapter(Context context, List<ImageBean> imageIdList, AlertDialog dialog) {
        this.context = (BaseActivity) context;
        this.imageIdList = imageIdList;
        this.mDialog = dialog;
        bitmapUtils = new BitmapUtils(context);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = (ViewGroup) View.inflate(context, R.layout.pop_adapter_layout, null);
        RoundImageView imageView = (RoundImageView) view.findViewById(R.id.iv_pop);
        imageView.setScaleType(ScaleType.CENTER_CROP); //CENTER_CROP按比例扩大
        imageView.setImageResource(R.drawable.banner_empty);

        int index = position % imageIdList.size();
        // ((ViewPager)container).addView(iv);
        if (((ViewPager) container).getChildCount() == imageIdList.size()) {
            ((ViewPager) container).removeView(view);
        }

        ((ViewPager) container).addView(view);
        final ImageBean pop_image = imageIdList.get(index);
        bitmapUtils.display(imageView, Constants.HOST + pop_image.getPicUrl());
        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isEmpty(pop_image.getUrl())) {
                    return;
                }
                mDialog.dismiss();
                Intent intent = new Intent(context, WebViewPage.class);
                intent.putExtra("url", pop_image.getUrl());
                intent.putExtra("title", pop_image.getName());
                intent.putExtra("BACK_MODE", WebViewPage.BACK_DIRECT); //webview的返回形式
                context.startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, false);
            }
        });
        return view;
    }

    @Override
    public int getCount() {
        return imageIdList.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }
}
